//graceChecker.js
/**
数据验证（表单验证）
来自 grace.hcoder.net 
作者 hcoder 深海
*/
export default {
	error: '',
	check: function(data, rules) {
		// Object.keys(data).forEach((value, key) => {
		// 	if (!rules[value]) {
		// 		return true;
		// 	}
		// 	if (!rules[value].required) {
		// 		return true;
		// 	}
		// 	if (!rules[value].message) {
		// 		return true;
		// 	}
		// 	// type类型:date/Array/Number
		// 	switch (rules[value].type) {
		// 		case '':
		// 			// var reg = new RegExp('^.{' + rules[i].checkRule + '}$');
		// 			if (!value) {
		// 				this.error = rules[value].message;
		// 				return false;
		// 			}
		// 			break;
		// 	}
		// })
		for (var i = 0; i < rules.length; i++) {
			if (!rules[i].checkType) {
				return true;
			}
			if (!rules[i].name) {
				return true;
			}
			if (!rules[i].message) {
				return true;
			}
			if (!data[rules[i].name]) {
				this.error = rules[i];
				return false;
			}
			switch (rules[i].checkType) {
				case 'string':
					var reg = new RegExp('^.{' + rules[i].checkRule + '}$');
					if (!reg.test(data[rules[i].name])) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'int':
					var reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + rules[i].checkRule + '}$');
					if (!reg.test(data[rules[i].name])) {
						this.error = rules[i];
						return false;
					}
					break;
					break;
				case 'between':
					if (!this.isNumber(data[rules[i].name])) {
						this.error = rules[i];
						return false;
					}
					var minMax = rules[i].checkRule.split(',');
					minMax[0] = Number(minMax[0]);
					minMax[1] = Number(minMax[1]);
					if (data[rules[i].name] > minMax[1] || data[rules[i].name] < minMax[0]) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'betweenD':
					var reg = /^-?[1-9][0-9]?$/;
					if (!reg.test(data[rules[i].name])) {
						this.error = rules[i];
						return false;
					}
					var minMax = rules[i].checkRule.split(',');
					minMax[0] = Number(minMax[0]);
					minMax[1] = Number(minMax[1]);
					if (data[rules[i].name] > minMax[1] || data[rules[i].name] < minMax[0]) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'betweenF':
					var reg = /^-?[0-9][0-9]?.+[0-9]+$/;
					if (!reg.test(data[rules[i].name])) {
						this.error = rules[i];
						return false;
					}
					var minMax = rules[i].checkRule.split(',');
					minMax[0] = Number(minMax[0]);
					minMax[1] = Number(minMax[1]);
					if (data[rules[i].name] > minMax[1] || data[rules[i].name] < minMax[0]) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'same':
					if (data[rules[i].name] != rules[i].checkRule) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'notsame':
					if (data[rules[i].name] == rules[i].checkRule) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'email':
					var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
					if (!reg.test(data[rules[i].name])) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'phoneno':
					var reg = /^1[0-9]{10,10}$/;
					if (!reg.test(data[rules[i].name])) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'zipcode':
					var reg = /^[0-9]{6}$/;
					if (!reg.test(data[rules[i].name])) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'reg':
					var reg = new RegExp(rules[i].checkRule);
					if (!reg.test(data[rules[i].name])) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'in':
					if (rules[i].checkRule.indexOf(data[rules[i].name]) == -1) {
						this.error = rules[i];
						return false;
					}
					break;
				case 'notnull':
					if (data[rules[i].name] == null || data[rules[i].name].length < 1) {
						this.error = rules[i];
						return false;
					}
					break;
			}
		}
		return true;
	},
	isNumber: function(checkVal) {
		var reg = /^-?[1-9][0-9]?.?[0-9]*$/;
		return reg.test(checkVal);
	}
}
